package iotest.byteio;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class OutputStreamDemo {
	public static void main(String[] args) {
		writebyte2();
	}

	private static void writebyte2() {
		OutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream("byte.txt");
//			outputStream = new FileOutputStream(new File("byte.txt"));
//			outputStream = new FileOutputStream(new File("byte.txt"),true);

			String string = "Hello Word";
			byte[] bs = string.getBytes();
			outputStream.write(bs,0,4);
			outputStream.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (outputStream != null) {
					outputStream.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private static void writebyte1() {
		OutputStream outputStream = null;
		try {
//			outputStream = new FileOutputStream(new File("byte.txt"));
			outputStream = new FileOutputStream(new File("byte.txt"),true);

			String string = "Hello Word";
			byte[] bs = string.getBytes();
			outputStream.write(bs);
			outputStream.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (outputStream != null) {
					outputStream.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private static void writebyte() {
		OutputStream outputStream = null;
		try {
//			outputStream = new FileOutputStream(new File("byte.txt"));
			outputStream = new FileOutputStream(new File("byte.txt"),true);

			String string = "Hello Word";
			byte[] bs = string.getBytes();
			for (byte b : bs) {
				outputStream.write(b);
			}
			outputStream.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (outputStream != null) {
					outputStream.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
